# Contributor: prspkt <prspkt@protonmail.com>
# Maintainer: prspkt <prspkt@protonmail.com>
pkgname=xscreensaver
pkgver=6.08
pkgrel=0
pkgdesc="Screensavers for X11 environment"
url="https://www.jwz.org/xscreensaver/"
arch="all"
license="GPL-2.0-or-later GPL-3.0-or-later 0BSD MIT"
depends="bc"
options="suid"
# perl-doc: for "perldiag.pod" used by "diagnostics" in "hacks/check-configs.pl"
makedepends="
	elogind-dev
	gdk-pixbuf-xlib-dev
	gettext-dev
	glu-dev
	gtk+2.0-dev
	gtk+3.0-dev
	intltool
	libjpeg-turbo-dev
	libxi-dev
	libxinerama-dev
	libxml2-dev
	libxmu-dev
	libxrandr-dev
	mesa-dev
	perl
	perl-doc
	"
subpackages="$pkgname-doc $pkgname-gl-extras:gl $pkgname-extras"
source="https://www.jwz.org/xscreensaver/xscreensaver-$pkgver.tar.gz
	dumb-pam-check.patch
	"

_libexecdir=/usr/libexec/xscreensaver
_confdir=/usr/share/xscreensaver/config

# secfixes:
#   6.02-r0:
#     - CVE-2021-34557

prepare() {
	default_prepare
	update_config_sub
}

build() {
	./configure \
		--build=$CBUILD \
		--host=$CHOST \
		--prefix=/usr \
		--sysconfdir=/etc \
		--mandir=/usr/share/man \
		--infodir=/usr/share/info \
		--with-gtk \
		--with-xinerama-ext \
		--with-xf86vmode-ext \
		--with-randr-ext \
		--with-elogind
	make
}

package() {
	make install_prefix="$pkgdir" install
}

gl() {
	local _conf
	pkgdesc="An enhanced set of screensavers that require OpenGL"
	cd "$pkgdir"
	mkdir -p "$subpkgdir"/$_confdir
	scanelf -Rn . | awk '$2 ~ /libE?GL/ { print $3 }' | while read -r f; do
		mkdir -p "$subpkgdir"/${f%/*}
		mv "$f" "$subpkgdir"/${f%/*}

		_conf=usr/share/xscreensaver/config/${f##*/}.xml
		if [ -f "$_conf" ]; then
			mv "$_conf" "$subpkgdir"/$_confdir
		fi
	done
}

extras() {
	pkgdesc="An enhanced set of screensavers"
	mkdir -p "$subpkgdir/$_libexecdir" "$subpkgdir/$_confdir"

	local _path
	for _path in "$pkgdir/$_libexecdir"/*; do
		expr "$_path" : \
			'.*/xscreensaver-\(auth\|gfx\)$' > /dev/null || \
			mv "$_path" "$subpkgdir/$_libexecdir"
	done

	mv "$pkgdir/$_confdir"/* "$subpkgdir/$_confdir"
}

sha512sums="
a333d4921ed3a8219f3b672dd68543aea31fcd64ea6766143f1c7f6e5ed3bc7122355635d5afba1632457ad71a11cabdc7b2756431e4f1dc7e0426ae0896e6db  xscreensaver-6.08.tar.gz
3e0dd718964e6b34c6dd35c67550be4a8e5062f03bc443499e3663b1c1fa0fdb77f68dff74e3a0aabe337b9eae0dc32be823894483d42965ef3c4f01a6c311ff  dumb-pam-check.patch
"
